package main

import (
	"github.com/pebbe/zmq4"
	"log"
	// "time"
)

func main() {
	log.SetFlags(log.Lshortfile)
	ce := func(err error) {
		if err != nil {
			log.Panic(err)
		}
	}
	server := func() {
		context, err := zmq4.NewContext()
		ce(err)
		responder, err := context.NewSocket(zmq4.REP)
		ce(err)
		err = responder.Bind("tcp://0.0.0.0:5555")
		ce(err)
		for {
			_, err = responder.RecvMessage(zmq4.SNDMORE)
			ce(err)
			// log.Println("server redv", request)
			// time.Sleep(time.Second)
			_, err = responder.SendMessage("world")
			ce(err)
		}
	}
	i := 0
	client := func() {
		context, err := zmq4.NewContext()
		ce(err)
		responder, err := context.NewSocket(zmq4.REQ)
		ce(err)
		err = responder.Connect("tcp://127.0.0.1:5555")
		ce(err)
		for {
			i++
			if i%1000 == 0 {
				log.Println(i)
			}
			_, err := responder.SendMessage("hello")
			ce(err)
			_, err = responder.RecvMessage(zmq4.SNDMORE)
			ce(err)
			// log.Println("client recv", request)
		}
	}
	go server()
	client()
}
